  
package com.sg.common;  

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sg.common.exception.BaseBusinessException;
/**  
 * ClassName:ExceptionController <br/> 
 * description：用于处理controller层抛出的异常，根据 @ExceptionHandler来适配具体的异常类型，可适配自定义扩展异常（需添加对应自定义异常捕获方法）
 * Date:     2017年12月8日 下午2:08:12 <br/>  
 * @author   xzk 
 * @version  1.0 
 * @since    JDK 1.8  
 * @see        
 */
@ControllerAdvice
public class ControllerExceptionHandler {
	
	private Logger logger = Logger.getLogger("exception");
	
	/**
	 * 
		 * defaultExceptionProcess:默认异常处理，主要对底层异常错误进行处理. <br/>  
		 *  
		 * @author xzk  
		 * @param request
		 * @param response
		 * @param throwable
		 * @return  
		 * @since JDK 1.8
	 */
	@ExceptionHandler({Error.class, Exception.class, Throwable.class})
	public @ResponseBody ResultModel defaultExceptionProcess(HttpServletRequest request,HttpServletResponse response,Throwable throwable){
		
		logger.error(throwable);
		
		return new ResultModel(Boolean.FALSE,"",throwable.getMessage());
	}
	
	/**
	 * 
		 * defaultExceptionProcess:默认异常处理，主要对底层异常错误进行处理. <br/>  
		 *  
		 * @author xzk  
		 * @param request
		 * @param response
		 * @param throwable
		 * @return  
		 * @since JDK 1.8
	 */
	@ExceptionHandler({BaseBusinessException.class})
	public @ResponseBody ResultModel businessExceptionProcess(HttpServletRequest request,HttpServletResponse response,BaseBusinessException bussinessException){

		logger.error(bussinessException);
		
		return new ResultModel(Boolean.FALSE,bussinessException.getErrorCode(),bussinessException.getErrorMessage());
	}
	
}
  
	